//
//  RHInvParamSelectCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/6/14.
//

#import "RHInvParamSelectCell.h"
#import "RHInvParamSelectItemCell.h"
#import "RHInvItemHeaderView.h"

#define cellLeftMargin 60
@interface RHInvParamSelectCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) QMUILabel *titleLab;

@property (nonatomic, strong) UICollectionView *collectionView;
@end
@implementation RHInvParamSelectCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(void)setCellModel:(RHInvParamModel *)cellModel
{
    _cellModel = cellModel;
    self.titleLab.text = cellModel.title.rh_localized;
    NSInteger rowCount = cellModel.rowCount;//每行的个数
    __block CGFloat collectViewHeight = 0 ;
    [cellModel.rowArr enumerateObjectsUsingBlock:^(RHInvParamModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"---->collection 组头:%@",obj.title);
        CGFloat itemSetionHeight = obj.title.length > 0?30:0;
        NSLog(@"----->collectin组头高度:%f",itemSetionHeight);
        NSInteger itemRow = obj.itemArr.count % rowCount? (obj.itemArr.count / rowCount)+1 : (obj.itemArr.count / rowCount);
        CGFloat itemRowHeight = itemRow * 45;
        collectViewHeight += itemSetionHeight;
        collectViewHeight += itemRowHeight;
    }];
    //加个底部间距
    collectViewHeight += 10;
    NSLog(@"----->collectin高度:%f",collectViewHeight);
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(collectViewHeight);
    }];
    [self.collectionView reloadData];
}

- (void)setupUI{
    kWeakSelf;
    self.contentView.backgroundColor = kCOLOR_HEX(@"#F4F6F9");
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    [bgView rounded:8 width:1 color:kBorderColor];
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));

    }];
    
    _titleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#3F4443")];
    _titleLab.textAlignment = NSTextAlignmentLeft;
    [bgView addSubview:_titleLab];
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
        make.top.mas_offset(15);
        
    }];
    
    UIView *line = kView;
    line.backgroundColor = kCOLOR_HEX(@"#EBEDF0");
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.titleLab);
        make.top.equalTo(weakSelf.titleLab.mas_bottom).offset(10);
        make.height.offset(1);
    }];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    layout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    [_collectionView registerClass:[RHInvParamSelectItemCell class] forCellWithReuseIdentifier:@"RHInvParamSelectItemCell"];
    [_collectionView registerClass:[RHInvItemHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"RHInvItemHeaderView"];
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    [bgView addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.top.equalTo(line.mas_bottom);
        make.bottom.mas_offset(0);
    }];
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    RHInvParamModel *model = self.cellModel.rowArr[section];
    if (model.title.length == 0) {
        return CGSizeMake(kUI_WINDOW_W - cellLeftMargin, 5);;
    }
    return CGSizeMake(kUI_WINDOW_W-cellLeftMargin, 30);
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    NSLog(@"----->返回%ld组",self.cellModel.rowArr.count);
    return self.cellModel.rowArr.count;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    RHInvParamModel *model = self.cellModel.rowArr[indexPath.section];
    RHInvItemHeaderView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"RHInvItemHeaderView" forIndexPath:indexPath];
    headView.titleLab.text = model.title.rh_localized;
    return headView;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    RHInvParamModel *model = self.cellModel.rowArr[section];
    NSLog(@"----->返回%ld个item",model.itemArr.count);
    return model.itemArr.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RHInvParamModel *model = self.cellModel.rowArr[indexPath.section];
    NSLog(@"section = %ld  row = %ld  item = %ld",indexPath.section ,indexPath.row,indexPath.item);
    RHInvParamSelectItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RHInvParamSelectItemCell" forIndexPath:indexPath];
    cell.itemModel = model.itemArr[indexPath.item];
    cell.contentView.backgroundColor = UIColor.yellowColor;
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat itemWidth = (kUI_WINDOW_W - cellLeftMargin - 15*4 - (self.cellModel.rowCount-1)*15)/self.cellModel.rowCount-1;
    return CGSizeMake(itemWidth , 45);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 15, 0, 15);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    RHInvParamModel *model = self.cellModel.rowArr[indexPath.section];
    RHInvParamModel *itemModel = model.itemArr[indexPath.item];
    if (self.selectBlock) {
        self.selectBlock(itemModel);
    }

//    [collectionView reloadItemsAtIndexPaths:@[indexPath]];
}

@end
